home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wpjv1n1.zip / TRASH.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-03  |  6KB  |  213 lines

  1. (*****************************************)
  2. (*                                       *)
  3. (*   Trashcan   (trash.pas)              *)
  4. (*                                       *)
  5. (*   Copyright (c) 1992,93 by A. Furrer  *)
  6. (*                                       *)
  7. (*   This program requires Windows 3.1   *)
  8. (*****************************************)
  9.  
  10. program Trashcan;
  11.  
  12. {$M 1024,0}
  13. {$R-}       {no range checking}
  14. {$S-}       {no stack checking}
  15. {$I-}       {no i/o checking}
  16. {$D-}       {no debug informations}
  17. {$L-}       {no local symbols}
  18.  
  19. {$R TRASH.RES}
  20.  
  21. uses Strings, WinTypes, WinProcs, WObjects, WinDOS;
  22.  
  23. const cm_About = 1;
  24.  
  25. (****************************************)
  26. (* Declarations for Drag&Drop-functions *)
  27. (****************************************)
  28.  
  29. const
  30.   wm_DropFiles = $0233;
  31.  
  32. function DragQueryFile(Drop: THandle;FileIndex: Word;
  33.                        FileName: PChar;MaxChars: Word):Word; external 'SHELL'
  34.  index 11;
  35. function DragQueryPoint(Drop: THandle;var Pt: TPoint):Bool;  external 'SHELL'
  36.  index 13;
  37. procedure DragFinish(Drop: THandle);                         external 'SHELL'
  38.  index 12;
  39. procedure DragAcceptFiles(Wnd: HWnd;Accept: Bool);           external 'SHELL'
  40.  index 9;
  41.  
  42. (****************)
  43. (* Mainwindow   *)
  44. (****************)
  45.  
  46. type
  47.   PMainwindow = ^TMainwindow;
  48.   TMainwindow =object(TWindow)
  49.     procedure SetupWindow; virtual;
  50.     function CanClose: Boolean; virtual;
  51.     function  GetClassName : PChar;virtual;
  52.     procedure GetWindowClass(var AWndClass:TWndClass);virtual;
  53.     procedure WMQueryOpen(var Msg : TMessage);virtual wm_First+wm_QueryOpen;
  54.     procedure WMSysCommand(var Msg : TMessage); virtual wm_First +
  55.  wm_SysCommand;
  56.     procedure WMDropFiles(var Msg : TMessage); virtual wm_first + wm_DropFiles;
  57.     procedure Delete(s : PChar);
  58.   end;
  59.  
  60. procedure TMainwindow.SetupWindow;
  61. begin
  62.   TWindow.SetupWindow;
  63.   (* append 'About' to Trashcabs systemmenu *)
  64.   AppendMenu(GetSystemMenu(HWindow,FALSE),
  65.   mf_Separator,0,NIL);
  66.   AppendMenu(GetSystemMenu(HWindow,FALSE),
  67.   mf_ByCommand or mf_String,cm_About,
  68.   'About Trashcan...');
  69.   (* register mainwindow to accept Drag&Drop *)
  70.   DragAcceptFiles(HWindow,TRUE);
  71.  
  72. end;
  73.  
  74. function TMainwindow.CanClose;
  75. begin
  76.   if TWindow.CanClose then
  77.     (* Unregister mainwindow for Drag&Drop *)
  78.     DragAcceptFiles(HWindow,false);
  79.   CanClose:=true;
  80. end;
  81.  
  82. function TMainwindow.GetClassName :PChar;
  83. begin
  84.   GetClassName:='Trashcan';
  85. end;
  86.  
  87. procedure TMainwindow.GetWindowClass;
  88. begin
  89.   TWindow.GetWindowClass(AWndClass);
  90.   AWndClass.HIcon:=LoadIcon(HInstance, 'Trashcan');
  91. end;
  92.  
  93. procedure TMainwindow.WMQueryOpen;
  94. begin
  95.   Msg.Result:=0; (* so trashcan will always be an icon *)
  96. end;
  97.  
  98. procedure TMainwindow.WMSysCommand;
  99. begin
  100.    if Msg.wParam = cm_About then (* show about dialog *)
  101.      Application^.ExecDialog(New(PDialog,Init(@self,'About')));
  102.    DefWndProc(Msg);
  103. end;
  104.  
  105.  
  106. (* this is the Drag&Drop procedure *)
  107. procedure TMainwindow.WMDropFiles;
  108. var s : array[0..255] OF char;
  109.     i,number : word;
  110. begin
  111.   (* get number of dropped filenames *)
  112.   number:=DragQueryFile(Msg.wParam,$ffff,nil,0);
  113.  
  114.   (* get each dropped filename *)
  115.   (* and call the delete function *)
  116.   for i:=0 TO number-1 DO begin
  117.     DragQueryFile(Msg.wParam,i,s,SizeOf(s));
  118.     Delete(s);
  119.   end;
  120.  
  121.   (* dispose internal Drag&Drop memory *)
  122.   DragFinish(Msg.wParam);
  123. end;
  124.  
  125.  
  126. (* Function to delete a file or a directory *)
  127. procedure TMainwindow.Delete(s : PChar);
  128. var F : file;
  129.     Attribut : word;
  130.     DirInfo: TSearchRec;
  131.     ss : Array[0..255] of char;
  132.     deleteOK : boolean;
  133.  
  134. begin
  135.   Assign(F,s);
  136.   GetFAttr(F,Attribut);
  137.   if Attribut and (faReadOnly or faHidden or faSysFile)<>0 then begin
  138.   (* file is hidden, system or readonly *)
  139.     if Attribut and faDirectory <>0 then
  140.       StrCopy(ss,'The directory ')
  141.     else
  142.       StrCopy(ss,'The file ');
  143.     StrCat(ss,s);
  144.     StrCat(ss,' is read only, hidden or a system file.'#13'Do you really want to
  145.  delete it?');
  146.     if MessageBox(HWindow,ss,'Trashcan',mb_YesNo or mb_IconQuestion)=id_yes then
  147.  begin
  148.       (* clear readonly attribut *)
  149.       Attribut:=Attribut and not faReadOnly;
  150.       SetFAttr(F,Attribut);
  151.       deleteOK:=TRUE;
  152.     end
  153.     else
  154.       deleteOK:=FALSE;
  155.   end
  156.   else
  157.     deleteOK:=TRUE;
  158.   if deleteOK then begin
  159.     (* this will delete the file or directory *)
  160.     if Attribut and faDirectory <>0 then begin
  161.       (* delete all files in this directory recursive *)
  162.       (* and at the end the directory itself          *)
  163.       StrCopy(ss,s);
  164.       StrCat(ss,'\*.*');
  165.       FindFirst(ss, faAnyFile, DirInfo);
  166.       while DosError = 0 do begin
  167.         if (StrComp(DirInfo.Name,'.')<>0)
  168.         and (StrComp(DirInfo.Name,'..')<>0) then begin
  169.           StrCopy(ss,s);
  170.           StrCat(ss,'\');
  171.           StrCat(ss,DirInfo.Name);
  172.           Delete(ss);
  173.         end;
  174.         FindNext(DirInfo);
  175.       end;
  176.       RemoveDir(s);
  177.     end
  178.     else Erase(F);
  179.   end;
  180. end;
  181.  
  182. (************************)
  183. (* TTrashcanApplication *)
  184. (************************)
  185.  
  186. type
  187.    TTrashcanApplication =
  188.      object(TApplication)
  189.        procedure InitMainWindow; virtual;
  190.      end;
  191.  
  192. procedure TTrashcanApplication.InitMainWindow;
  193. begin
  194.   MainWindow := New(PMainwindow,Init(nil, 'Trashcan'));
  195. end;
  196.  
  197. (***************)
  198. (* Mainprogram *)
  199. (***************)
  200.  
  201. var
  202.    Prg : TTrashcanApplication;
  203. begin
  204.    if HPrevInst=0 then begin (* start only one instance *)
  205.      CmdShow := sw_Minimize; (* start as icon *)
  206.      Prg.Init('Trashcan');
  207.      Prg.Run;
  208.      Prg.Done;
  209.   end;
  210. end.
  211.  
  212. 
  213.